package org.example.skytool.commom;

import lombok.extern.slf4j.Slf4j;
import org.example.exception.ImageEroorException;
import org.springframework.web.multipart.MultipartFile;


@Slf4j
public class FileUtil {
    public static final Integer IMAGE_FILE_LIMIT_SIZE = 2 * 1024 * 1024;

    public static String checkAvatarImage(MultipartFile file) {
        if (StringUtil.isEmpty(file.getOriginalFilename())) {
            throw new ImageEroorException( "参数错误");
        }
        //判断文件大小
        long fileSize = file.getSize();
        if (fileSize < 10 || fileSize > IMAGE_FILE_LIMIT_SIZE) {
            throw new ImageEroorException( "头像大小不得大于2MB");
        }
        String picType;
        try {
            picType = ImageTypeUtil.getPicType(file.getInputStream());
        } catch (Exception e) {
            throw new ImageEroorException("文件读取失败");
        }
        if (!ImageTypeUtil.PERMIT_IMAGE_TYPES.contains(picType)) {
            throw new ImageEroorException("请上传jpg,png,webp格式的图片");
        }
        return picType;
    }

    public static String getSuffixByMimeType(String mimetype) {
        return ImageTypeUtil.getSuffixByMimeType(mimetype);
    }
}
